import matplotlib.pyplot as plt
import numpy as np
import sympy as sp


def set_chinese_font():
    """设置中文字体"""
    plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
    plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

set_chinese_font()


def inequalities_and_complex():
    print("=== 不等式与复数 ===")
    
    # 不等式求解
    x = sp.Symbol('x')
    inequality1 = 2*x + 3 > 7
    inequality2 = x**2 - 4 <= 0
    
    print(f"不等式 {inequality1}")
    solution1 = sp.solve(inequality1, x)
    print(f"解集: {solution1}")
    
    print(f"\n不等式 {inequality2}")
    solution2 = sp.solve(inequality2, x)
    print(f"解集: {solution2}")
    
    # 复数运算
    z1 = 2 + 3j
    z2 = 1 - 2j
    
    print(f"\n复数 z1 = {z1}")
    print(f"复数 z2 = {z2}")
    print(f"加法: z1 + z2 = {z1 + z2}")
    print(f"乘法: z1 × z2 = {z1 * z2}")
    print(f"除法: z1 ÷ z2 = {z1 / z2}")
    print(f"模: |z1| = {abs(z1):.2f}")
    print(f"辐角: arg(z1) = {np.angle(z1):.2f} rad")
    
    # 复平面可视化
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
    
    # 不等式解集可视化
    x_vals = np.linspace(-3, 3, 400)
    y_vals = x_vals**2 - 4
    ax1.plot(x_vals, y_vals, 'b-', label='y = x² - 4')
    ax1.fill_between(x_vals, y_vals, -5, where=(y_vals <= 0), alpha=0.3, color='red')
    ax1.axhline(0, color='black', linewidth=0.5)
    ax1.axvline(0, color='black', linewidth=0.5)
    ax1.set_title('不等式 x² - 4 ≤ 0 的解集')
    ax1.grid(True)
    ax1.legend()
    
    # 复平面
    complex_points = [z1, z2, z1 + z2, z1 * z2]
    colors = ['red', 'blue', 'green', 'purple']
    labels = ['z1', 'z2', 'z1+z2', 'z1×z2']
    
    for point, color, label in zip(complex_points, colors, labels):
        ax2.plot(point.real, point.imag, 'o', color=color, label=label, markersize=8)
    
    ax2.axhline(0, color='black', linewidth=0.5)
    ax2.axvline(0, color='black', linewidth=0.5)
    ax2.set_xlim(-3, 6)
    ax2.set_ylim(-4, 4)
    ax2.set_aspect('equal')
    ax2.set_title('复平面')
    ax2.grid(True)
    ax2.legend()
    
    plt.tight_layout()
    plt.show()

inequalities_and_complex()